﻿using Itech_Run.Application.Shared;
using Itech_Run.Blog.Application.Contracts.Dto;
using Itech_Run.Blog.Application.Contracts.Service;
using Itech_Run.Blog.Domain.Entity;
using System.Linq.Expressions;
using System.Linq.Dynamic.Core;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Itech_Run.Blog.Application.Service
{
    public class BlogInfoService : BaseApplicationService<Blog_Info>, IBlogInfoService
    {
        private readonly IRepository<Blog_Info, string> _blogInfoRepository;
        public BlogInfoService(IRepository<Blog_Info, string> blogInfoRepository)
        {
            _blogInfoRepository = blogInfoRepository;
        }
        public async Task DeleteAsync(string id, string operateUser)
        {
            try
            {
                var blogInfo = await _blogInfoRepository.GetAsync(id, includeDetails: true);
                SetUpdateBy(blogInfo, operateUser);
                blogInfo.IsDel = true;
                await _blogInfoRepository.UpdateAsync(blogInfo);
            }
            catch { }
        }

        public async Task<BlogInfoVerboseDto> GetAsync(string id)
        {
            var blogInfo = await _blogInfoRepository.GetAsync(id, includeDetails: true);
            return ObjectMapper.Map<Blog_Info, BlogInfoVerboseDto>(blogInfo);
        }

        public async Task<PagedResultDto<BlogInfoDto>> GetListAsync(BlogInfoListDto listDto)
        {
            string topSort = "IsTop desc,";//置顶
            if (string.IsNullOrEmpty(listDto.Sorting)) listDto.Sorting = "CreatedTime desc";
            listDto.Sorting = topSort + listDto.Sorting;
            Expression<Func<Blog_Info, bool>> curExpression = s => s.IsDel == false;
            if (!string.IsNullOrEmpty(listDto.Title))
                curExpression = curExpression.And(s => s.Title.Contains(listDto.Title));
            if (listDto.IsPublish != null)
                curExpression = curExpression.And(s => s.IsPublish == listDto.IsPublish);

            var query = (await _blogInfoRepository.WithDetailsAsync()).WhereIf(true, curExpression).OrderBy(listDto.Sorting);
            var items = ObjectMapper.Map<List<Blog_Info>, List<BlogInfoDto>>(query.PageBy(listDto.SkipCount, listDto.MaxResultCount).ToList());

            return new PagedResultDto<BlogInfoDto>(query.Count(), items);
        }

        public async Task<BlogInfoDto> InsertAsync(BlogInfoCreateDto dto, string operateUser)
        {
            var blogInfo = ObjectMapper.Map<BlogInfoCreateDto, Blog_Info>(dto);
            SetInsertBy(blogInfo, operateUser);
            if (dto.PersonCategoryList.Count > 0)
                blogInfo.AllocatePersonCategory(dto.PersonCategoryList);
            if (dto.PersonLabelList.Count > 0)
                blogInfo.AllocatePersonLabel(dto.PersonLabelList);
            blogInfo.Author = operateUser;
            blogInfo.IsDel = false;
            var result = await _blogInfoRepository.InsertAsync(blogInfo);
            return ObjectMapper.Map<Blog_Info, BlogInfoDto>(result);
        }

        public async Task<BlogInfoDto> UpdateAsync(BlogInfoUpdateDto dto, string operateUser)
        {
            var blogInfo = await _blogInfoRepository.GetAsync(dto.Id, includeDetails: true);
            ObjectMapper.Map(dto, blogInfo);
            SetUpdateBy(blogInfo, operateUser);
            blogInfo.IsDel = false;
            blogInfo.AllocatePersonCategory(dto.PersonCategoryList);
            blogInfo.AllocatePersonLabel(dto.PersonLabelList);
            var result = await _blogInfoRepository.UpdateAsync(blogInfo);
            return ObjectMapper.Map<Blog_Info, BlogInfoDto>(result);
        }
    }
}
